package com.moxiao.yfjh.module.activity.service.packageProduct;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.activity.controller.admin.packageProduct.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageProduct.PackageProductDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.packageProduct.PackageProductConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.packageProduct.PackageProductMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲套餐商品 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class PackageProductServiceImpl implements PackageProductService {

    @Resource
    private PackageProductMapper packageProductMapper;

    @Override
    public void createPackageProduct(List<PackageProductCreateReqVO> createReqVO) {
        // 插入
        List<PackageProductDO> productDOS = PackageProductConvert.INSTANCE.convert(createReqVO);
        packageProductMapper.insertBatch(productDOS);
    }

    @Override
    public void updatePackageProduct(List<PackageProductUpdateReqVO> updateReqVO) {
        updateReqVO.forEach(packageProductUpdateReqVO -> {
            // 校验存在
            validatePackageProductExists(packageProductUpdateReqVO.getId());
            // 更新
            PackageProductDO updateObj = PackageProductConvert.INSTANCE.convert(packageProductUpdateReqVO);
            packageProductMapper.updateById(updateObj);
        });

    }

    @Override
    public void deletePackageProduct(Long id) {
        // 校验存在
        validatePackageProductExists(id);
        // 删除
        packageProductMapper.deleteById(id);
    }

    private void validatePackageProductExists(Long id) {
        if (packageProductMapper.selectById(id) == null) {
            throw exception(PACKAGE_PRODUCT_NOT_EXISTS);
        }
    }

    @Override
    public PackageProductDO getPackageProduct(Long id) {
        return packageProductMapper.selectById(id);
    }

    @Override
    public List<PackageProductDO> getPackageProductList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return packageProductMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PackageProductDO> getPackageProductPage(PackageProductPageReqVO pageReqVO) {
        return packageProductMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PackageProductDO> getPackageProductList(PackageProductExportReqVO exportReqVO) {
        return packageProductMapper.selectList(exportReqVO);
    }

}
